/**
 * 
 */
package br.com.games.daedal.imp;

import java.util.ArrayList;
import java.util.List;

import br.com.games.daedal.Box;
import br.com.games.daedal.DaedalTeam;
import br.com.games.daedal.Vertex;
import br.com.games.hefesto.board.Board;

/**
 * Tabuleiro
 * 
 * @since 2013
 * @author <a href="mailto:fernando.schindler@gmail.com">Fernando Schindler</a>
 */
public class DaedalBoard implements Board<Box> {

	/**
	 * Criar um novo board
	 * 
	 * @param size 
	 */
	public DaedalBoard(int size) {
		this(size, size, false);
	} 
	
	/**
	 * Criar um novo board
	 * 
	 * @param size 
	 */
	public DaedalBoard(int lines, int columns) {
		this(lines, columns, false);
	}
	
	/**
	 * Criar um viewer do tabukeiro de jogo
	 * @param lines
	 * @param columns
	 * @param isClassicMode
	 */
	public DaedalBoard(int lines, int columns, boolean isClassicMode) {
		this.boxes = new ArrayList<Box>();
		this.vertexes = new ArrayList<Vertex>();
		DaedalVertex [] verts = new DaedalVertex [4];
		for (int i = 0; i < columns; i++) {
			for (int j = 0; j < lines; j++) {
				verts[0] = new DaedalVertex(VertexType.VERTICAL,   i, j);
				verts[1] = new DaedalVertex(VertexType.VERTICAL,   i+1, j);
				verts[2] = new DaedalVertex(VertexType.HORIZONTAL, i, j);
				verts[3] = new DaedalVertex(VertexType.HORIZONTAL, i, j+1);
				
				for (int k = 0; k < verts.length; k++){
					int toShare = this.vertexes.indexOf(verts[k]);
					
					if (toShare >= 0){
						verts[k] = (DaedalVertex) this.vertexes.get(toShare);
					}
					else
						this.vertexes.add(verts[k]);
				} 
				
				this.boxes.add(new DaedalBox(verts.clone(), isClassicMode));
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.games.hefesto.board.Board#getSquares()
	 */
	@Override
	public List<Box> getSquares() {
		return this.boxes;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.games.hefesto.board.Board#reset()
	 */
	@Override
	public void reset() {

		for (Vertex v : vertexes) {
			v.setActive(false);
		}

		for (Box b : boxes) {
			b.setOwener(DaedalTeam.NONE);
			b.reset();
		}
	}

	/**
	 * 
	 * @return
	 */
	public List<Vertex> getVertexes() {
		return vertexes;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.games.hefesto.State#setAnalyzeMode(java.lang.Boolean)
	 */
	@Override
	public void setAnalyzeMode(Boolean value) {
		for (Vertex v : this.vertexes){
			v.setAnalyzeMode(value);
		}
	}

	/**
	 * Campos privados
	 */
	private List<Box> boxes;
	private List<Vertex> vertexes;
}
